package cn.tanghom.app.system.controller;

import cn.tanghom.content.model.Dashboard;
import cn.tanghom.content.repository.DashboardRepository;
import cn.tanghom.app.system.model.Resources;
import cn.tanghom.app.system.model.User;
import cn.tanghom.app.system.service.ResourcesService;
import cn.tanghom.app.system.service.UserService;
import cn.tanghom.conf.CommonKey;
import cn.tanghom.support.hook.DashboardModule;
import cn.tanghom.support.hook.ModuleManager;
import cn.tanghom.support.tree.TreeObject;
import cn.tanghom.support.tree.TreeUtil;
import cn.tanghom.support.web.BaseController;

import com.mysql.jdbc.Connection;

import org.flowable.engine.IdentityService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.flowable.task.api.TaskQuery;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import static cn.tanghom.support.web.BaseController.THEME_PATH;
/**
 * dashboard控制器,
 *
 * @author tanghom
 */
@Controller
@RequestMapping("dashboard")
public class DashboardController extends BaseController<Dashboard>{
    @Resource
    private UserService userService;      
    
    @Resource
    DashboardRepository dashboardRepository;
    
    @Autowired
	IdentityService identityservice;   
   
    
    @Autowired
    private TaskService taskService;    
    
    /**
     * dashboard 
     *
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public String index(Model model) {    	
    	
    	Iterable<Dashboard> dashs = dashboardRepository.findAll();
    	List<Dashboard> list = new ArrayList<>(10);
    	
    	List<DashboardModule<?>> plugins = ModuleManager.getModules(Dashboard.class);		
		
    	for(Dashboard dash: dashs){
    		for (DashboardModule<?> plugin : plugins) {
    			try {
    				if (plugin.hook("view", dash, this)) {
    					
    					if(plugin.loadModel(dash)){
    						plugin.viewModel(dash, this);
    						list.add(dash);
    					}
    				}
    			} catch (Exception e) {
    				plugin.handleError("view", e);
    			}
    		}
    	}
    	
    	model.addAttribute("dashboards", list);
        return THEME_PATH + "/dashboard";
    }   
    

}
